{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合（set）是一个无序的不重复元素序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'apple', 'orange'}\n",
      "{'Google', 'Taobao', 'Runoob'}\n",
      "添加后的thisset  {'Google', 'Taobao', 'Facebook', 'Runoob'}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "可以使用大括号 { } 或者 set() 函数创建集合，注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。\n",
    "'''\n",
    "\n",
    "fruits = {'apple','banana','orange'}\n",
    "print(fruits)\n",
    "thisset = set((\"Google\", \"Runoob\", \"Taobao\"))\n",
    "print(thisset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合的基本操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加后的thisset  {'banana', 1, 2, 3, 'Google', 'Taobao', 'Facebook', 'Runoob'}\n",
      "更新后的thisset  {'banana', 1, 2, 3, 'Google', 'Taobao', 'Facebook', 'Runoob'}\n",
      "移除后的thisset  {'banana', 1, 2, 3, 'Google', 'Taobao', 'Runoob'}\n",
      "discard 移除后的thisset  {'banana', 1, 2, 3, 'Google', 'Taobao', 'Runoob'}\n",
      "pop 移除后的thisset  {1, 2, 3, 'Google', 'Taobao', 'Runoob'}\n"
     ]
    }
   ],
   "source": [
    "# 添加元素   将元素 x 添加到集合 s 中，如果元素已存在，则不进行任何操作。\n",
    "thisset.add(\"Facebook\")\n",
    "thisset.add(\"banana\")\n",
    "print('添加后的thisset ',thisset)\n",
    "\n",
    "# 还有一个方法，也可以添加元素，且参数可以是列表，元组，字典等，语法格式如下：\n",
    "thisset.update({1,2,3})\n",
    "print('更新后的thisset ',thisset)\n",
    "\n",
    "\n",
    "# 移除元素\n",
    "thisset.remove('Facebook')# 不存在会发生错误\n",
    "print('移除后的thisset ',thisset)\n",
    "\n",
    "# 还有一个方法也是移除集合中的元素，且如果元素不存在，不会发生错误\n",
    "thisset.discard('Facebook')\n",
    "print('discard 移除后的thisset ',thisset)\n",
    "\n",
    "# 也可以设置随机删除集合中的一个元素\n",
    "\n",
    "thisset.pop()\n",
    "print('pop 移除后的thisset ',thisset)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输出列表 第三个元素为  6\n",
      "[1, 3, '列表1', '列表2']\n"
     ]
    }
   ],
   "source": [
    "list = [1,3,6,\"列表1\",\"列表2\"]\n",
    "print(\"输出列表 第三个元素为 \",list[2])\n",
    "del list[2]# 删除第三个元素\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
